/**
 * 
 */
package com.pactera.integral.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pactera.integral.common.web.Response;
import com.pactera.integral.common.web.WebResponse;
import com.pactera.integral.dao.UserGroupDao;
import com.pactera.integral.entity.UserGroup;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.pactera.integral.bean.GroupBean;
import com.pactera.integral.common.jpa.criteria.Criteria;
import com.pactera.integral.common.jpa.criteria.Restrictions;
import com.pactera.integral.dao.AuthorityGroupDao;
import com.pactera.integral.dao.GroupDao;
import com.pactera.integral.entity.AuthorityGroup;
import com.pactera.integral.entity.Group;
import com.pactera.integral.service.GroupService;
import org.springframework.util.CollectionUtils;

/**
 * @author TempUser
 *
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {

	@Autowired
	private GroupDao groupDao;
	@Autowired
	private AuthorityGroupDao authorityGroupDao;
	@Autowired
	private UserGroupDao userGroupDao;
	/** 
	 * 添加或修改组
	 * @param group
	 * @return Boolean
	 */
	@Override
	public Boolean saveGroup(Group group) {
		Group existingEntity = groupDao.findByGroupId(group.getGroupId());
		if(existingEntity != null){
			existingEntity.setName(group.getName());
			existingEntity.setDescription(group.getDescription());
			groupDao.save(existingEntity);
		}else{
			group.setCreatedTime(new Date());
			groupDao.save(group);
		}
		authorityGroupDao.deleteGroupId(group.getGroupId());
		if(group.getGroupId() != null){
			if(group.getAuthorityIds() != null && !"".equals(group.getAuthorityIds())){
				for (String authorityId : group.getAuthorityIds()) {
					AuthorityGroup authorityGroup = new AuthorityGroup();
					authorityGroup.setGroupId(group.getGroupId());
					authorityGroup.setAuthorityId(authorityId);
					authorityGroup.setCreatedTime(new Date());
					authorityGroupDao.save(authorityGroup);
				}
			}
			return Boolean.TRUE;
		}
		
		return Boolean.FALSE;
	}

	 /**
     * 分页查询
     * @param param
     * @param page
     * @return Page<Group>
     */
	@Override
	public Page<Group> pageGroup(Map<String, Object> param, Pageable page) {
		Criteria<Group> criteria = new Criteria<Group>();
    	if(param != null){
    		criteria.add(Restrictions.like("name", (String)param.get("name")));
    	}
    	Page<Group> p= groupDao.findAll(criteria,page);
    	
		return p;
	}

	
	/**
	 * 组明细
	 * @param groupId
	 * @return
	 */
	@Override
	public GroupBean getGroup(String groupId) {
		GroupBean bean = new GroupBean();
		Group group = groupDao.findByGroupId(groupId);
		List<AuthorityGroup> list = authorityGroupDao.findByGroupId(groupId);
		if(group != null){
			bean.setGroupId(group.getGroupId());
			bean.setDescription(group.getDescription());
			bean.setName(group.getName());
		}
		String authorityIds = "";
		for (AuthorityGroup authorityGroup : list) {
			authorityIds = authorityIds + authorityGroup.getAuthorityId()+",";
		}
		bean.setAuthorityIds(authorityIds.split(","));
		
		return bean;
	}

	

	/**
	 * 小组下拉列表
	 * @return List<Map<String, String>>
	 */
	@Override
	public List<Map<String, String>> getAllGroup() {
		List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
		List<Group> list = groupDao.findAll();
		for (Group group : list) {
			Map<String, String> map =new HashMap<String, String>();
			map.put("name", group.getName());
			map.put("groupId", group.getGroupId());
			listMap.add(map);
		}
		
		return listMap;
	}

	@Override
	public Response deleteGroup(String groupId,String force) {

		if(StringUtils.isNotEmpty(force) && "force".equalsIgnoreCase(force)){
			groupDao.delete(groupId);
			WebResponse response = new WebResponse();
			response.setResponseCode("0");
			response.setMessage("delete successfully");
			return response;
		}

		List<AuthorityGroup> list = authorityGroupDao.findByGroupId(groupId);
		if(!CollectionUtils.isEmpty(list)){
			WebResponse response = new WebResponse();
			String ownAuth = "ownAuth";
			response.setResponseCode("0");
			response.setErrorCode(ownAuth);
			response.setMessage("this group own authority,did you make sure force delete it ?");
			return response;
		}

		List<UserGroup> byGroupId = userGroupDao.findByGroupId(groupId);
		if(!CollectionUtils.isEmpty(byGroupId)){
			WebResponse response = new WebResponse();
			String ownUser = "ownUser";
			response.setResponseCode("0");
			response.setErrorCode(ownUser);
			response.setMessage("this group own user,did you make sure force delete it ?");
			return response;
		}

		WebResponse response = new WebResponse();
		response.setResponseCode("-1");
		response.setMessage("delete failure.");
		return response;
	}

}
